<?php
function_exists('UnixPath')  || die('OK');

	//_admin_group = '_admin_group'; 
	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del');
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';

	$_GET['id']	= (int) $_GET['id'];

	$body['navlist'] = array(
		'list' => '管理员组栏列表',
		'add' => '添加管理员组',
	);

	$body['_url']	= '';
	$body['url']	= $html['url'].$body['_url'];



if( $_GET['act']=='add' ){
	$mod_B	= include(B_DIR . '/_res/tree/mod_B.php');
	$L['mod_checkbox']	= mod_checkbox($mod_B);

	$left_B	= include(B_DIR . '/_res/tree/left_B.php');
	$L['left_checkbox']	= left_checkbox($left_B);

	$L['tab_checkbox']	= tab_checkbox();
}


if( $_GET['act']=='aok' ){
	$id = E::$DB->insert('_admin_group', 'name');
	hash::save($_POST['power'], B_DIR . '/_res/power/' . $id . '.php');
	C::left($id);
	A::msg('添加组完成!', $_POST['jurl'] , 1, 1);
}


if( $_GET['act']=='edit' ){
	$html['_tpl_right_act'] = 'add';
	$L = E::$DB->one('_admin_group','id='.$_GET['id']);
	$L['id'] || msg('无效的节点', $_POST['jurl'], 3, 1 );


	$mod_B	= include(B_DIR . '/_res/tree/mod_B.php');
	$L['mod_checkbox']		=   mod_checkbox($mod_B);
	$left_B	= include(B_DIR . '/_res/tree/left_B.php');
	$L['left_checkbox']	=   left_checkbox($left_B);

	$L['tab_checkbox'] = tab_checkbox();

	$power	= include(B_DIR . '/_res/power/' . $L['id'] . '.php');
	$L['power_json']	= json_encode($power);
	
}


if( $_GET['act']=='eok' ){

	$L = E::$DB->one('_admin_group', 'id=' . $_GET['id']);
	$L['id'] || msg('无效的节点', $_POST['jurl'], 3, 1 );
	

	//$_POST['power']	= serialize($_POST['power']);
	E::$DB->update('_admin_group', 'name', 'id=' . $L['id']);	

	hash::save($_POST['power'], B_DIR . '/_res/power/' . $L['id'] . '.php');
	C::left($L['id']);
	A::msg('修改节点 '. $_POST['name'].' 完成!', '?mod=_admin&fun=group&act=list', 3, 1 );
}


if( $_GET['act'] == 'del' ){
	$L = E::$DB->one('','id=' . $_GET['id']);
	$L['id'] || msg('无效的节点', $_POST['jurl'], 3, 1 );
	E::$DB->query('DELETE FROM _admin_group where id=' . $L['id']);
	A::msg('删除节点 '.$L['name'].' 完成!', $_POST['jurl'], 3, 1 );
}


if( $_GET['act'] == 'list' ){
	$p = E::$DB->page('_admin_group');
	$L = E::$DB->all('_admin_group', '1 ' . $p['limit']);
}


function mod_checkbox(&$a, $tab = ''){
	foreach($a as $k=>$v){
		$dep = $v['nodeType'] + 1;
		$s .= $tab . '<label for="mod_' . $k . '"><input type="checkbox" name="power[1][' . $k . ']" value="1" id="mod_' . $k . '" />' . $v['nodeName'] . '</label>';
		if(is_array($v['child'])){
			$s .= $tab . '<div class="checkboxdep' . $dep . '">'.mod_checkbox($v['child'], $tab) . $tab . '</div>';
		}
	}
	return $s;
}



function left_checkbox(&$a, $tab=''){
	foreach($a as $k=>$v){
		if( $v['isHide'] ){
			continue;
		}
		$s .= $tab . '<label for="left_' . $k . '"><input type="checkbox" name="power[0][' . $k . ']" value="1" id="left_' . $k . '" />' . $v['name'] . '</label>';
		if(is_array($v['child'])){
			$s .= $tab . '<div class="checkboxdep' . $v['deep'] . '">' . left_checkbox($v['child'], $tab) . $tab . '</div>';
		}
	}
	return $s;
}



function tab_checkbox(){
	$L = E::$DB->all('_table_field', 'isPower=1');
	$A = array();
	foreach($L as $a){
		$tid = $a['tid'];
		$id = $a['id'];
		if( !isset($A['tid']) ){
			$A[$tid] = E::$DB->one('_table_list', 'id='.$tid);
		}
		$A[$tid]['field'][$id] = $a;
		$A[$tid]['field'][$id]['paramGroup']	= E::$DB->one('_super_param', 'id='.$a['bindId']);
		$A[$tid]['field'][$id]['child']		= E::$DB->all('_super_param', 'pid='.$a['bindId']);
	}
	$s = '';
	foreach($A as &$tab){
		$tid = $tab['id'];
		$s .= '<label for="tab_'.$tid.'"><input id="tab_'.$tid.'" type="checkbox" value="1" name="power2[tab]['.$tid.']"/> 表: '.$tab['description'].' => '.$tab['tableName'].'</label><div class="checkboxdep1">';
			foreach($tab['field'] as &$field){
				$fid = $field['id'];
				$s .= '<label for="tab_'.$tid.'_'.$fid.'"><input id="tab_'.$tid.'_'.$fid.'" type="checkbox" value="1" name="power2[tab]['.$tid.']['.$fid.']"/>字段: '.$field['fieldName'].' => '.$field['fieldKey'].' 参数: '.$field['paramGroup']['paramName'].' => '.$field['paramGroup']['paramKey'].'</label><div class="checkboxdep2">';
				foreach($field['child'] as $k=>&$v){
					$pid = $v['id'];
					$s .= ' <label for="tab_'.$tid.'_'.$fid.'_'.$pid.'"><input type="checkbox" name="power[2]['.$tid.']['.$fid.']['.$pid.']"  value="1"  id="tab_'.$tid.'_'.$fid.'_'.$pid.'"/>'.$v['paramName'].'</label>';
				}
				unset($v);
				$s .= '</div>';
			}
			unset($field);
		$s .= '</div>';
	}
	unset($tab);
	return $s;
}
